package cn.cslg.tieba.service.impl;

import cn.cslg.tieba.domain.dao.FloorDao;
import cn.cslg.tieba.domain.dao.TieDao;
import cn.cslg.tieba.domain.entity.Floor;
import cn.cslg.tieba.domain.entity.Tie;
import cn.cslg.tieba.service.WriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Wu Zhenwei
 * 2017/11/21
 */
@Service
public class WriteServiceImpl implements WriteService {

    @Autowired
    private TieDao tieDao;

    @Autowired
    private FloorDao floorDao;

    @Transactional
    @Override
    public Tie addTie(Tie tie, String content) {
        tieDao.insert(tie);
        Floor floor = new Floor();
        floor.setContent(content);
        floor.setUserId(tie.getUserId());
        floor.setFloorNo(1);
        floor.setTieId(tie.getTieId());
        addFloor(floor);
        tie = tieDao.findById(tie.getTieId());
        return tie;
    }

    @Transactional
    @Override
    public Floor addFloor(Floor floor) {
        Tie tie = tieDao.findById(floor.getTieId());
        floor.setFloorNo(tie.getLastFloorNo() + 1);
        floorDao.insert(floor);
        tieDao.increaseLastFloor(floor.getTieId());
        floor = floorDao.findById(floor.getFloorId());
        return floor;
    }
}
